package fm.dongman.modellib.source

import android.util.Log
import fm.dongman.animefm.contractlib.BaseDataSource
import fm.dongman.animefm.contractlib.contract.RegisterContract
import fm.dongman.modellib.RemoteHelper
import fm.dongman.modellib.StaticArea.StaticUrl
import org.json.JSONObject

/**
 * 注册数据获取
 * Created by shize on 2017/11/14.
 */
class RegisterRepository : RegisterContract.IRegisterDataSource {
    override fun validatePhoneNumber(phone: String, type: Int, callback: BaseDataSource.LoadSourceCallback<String>) {
        val jsonObj = getValidateJSONObject(type, phone)
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(jsonObj.getString("message"))
    }

    /**
     * 获取验证 JSONObject
     * @param type 类型
     * @param phone 手机号
     * @return 返回的 JSONObject
     */
    private fun getValidateJSONObject(type: Int, phone: String): JSONObject? {
        return if (type == TYPE_MODIFY || type == TYPE_MODIFY_PHONE) {
            RemoteHelper.instance.parsePostUrl(StaticUrl.REGISTER_VALIDATE, mapOf(Pair("x-token", StaticUrl.TOKEN_KEY)), mapOf(Pair("phone", phone), Pair("type", type)))
        } else {
            RemoteHelper.instance.parsePostUrl(StaticUrl.REGISTER_VALIDATE, mapOf(Pair("phone", phone), Pair("type", type)))
        }
    }

    override fun registerAccount(phone: String, pwd: String, validateNum: String, callback: BaseDataSource.LoadSourceCallback<String>) {
        val jsonObj = RemoteHelper.instance.parsePostUrl(StaticUrl.REGISTER_ACCOUNT, mapOf(
                Pair("phone", phone), Pair("password", pwd), Pair("sms_code", validateNum)))
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(jsonObj.getString("message"))
    }

    override fun modifyPassword(phone: String, newPwd: String, validateNum: String, modifyType: Int, callback: BaseDataSource.LoadSourceCallback<String>) {
        // 修改密码
        if (modifyType == TYPE_MODIFY) {
            modifyPwd(phone, validateNum, newPwd, callback)
            return
        }
        // 忘记/重置密码
        if (modifyType == TYPE_FORGET) {
            forgetPwd(phone, validateNum, newPwd, callback)
            return
        }
        Log.e(javaClass.name, "modifyType = $modifyType 参数值为非指定参数错误！该参数只能为 TYPE_MODIFY = $TYPE_MODIFY 或 TYPE_FORGET = $TYPE_FORGET 之一！")
        callback.onDataLoadFiled()
    }

    override fun bindPhone(phone: String, validateNum: String, callback: BaseDataSource.LoadSourceCallback<String>) {
        val jsonObj = RemoteHelper.instance.parsePostUrl(StaticUrl.BIND_PHONE, mapOf(
                Pair("x-token", StaticUrl.TOKEN_KEY)), mapOf(Pair("phone", phone), Pair("sms_code", validateNum)))
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(jsonObj.getString("message"))
    }

    /**
     * 修改密码
     * @param phone 手机号
     * @param validateNum 验证码
     * @param newPwd 新密码
     * @param callback 回调
     */
    private fun modifyPwd(phone: String, validateNum: String, newPwd: String, callback: BaseDataSource.LoadSourceCallback<String>) {
        val jsonObj = RemoteHelper.instance.parsePostUrl(StaticUrl.MODIFY_PWD, mapOf(Pair("x-token", StaticUrl.TOKEN_KEY)), mapOf(
                Pair("phone", phone), Pair("sms_code", validateNum), Pair("new_password", newPwd)))
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(jsonObj.getString("message"))
    }

    /**
     * 忘记/重置密码
     * @param phone 手机号
     * @param validateNum 验证码
     * @param newPwd 新密码
     * @param callback 回调
     */
    private fun forgetPwd(phone: String, validateNum: String, newPwd: String, callback: BaseDataSource.LoadSourceCallback<String>) {
        val jsonObj = RemoteHelper.instance.parsePostUrl(StaticUrl.FORGET_PWD, mapOf(
                Pair("phone", phone), Pair("sms_code", validateNum), Pair("new_password", newPwd)))
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(jsonObj.getString("message"))
    }

    override fun getData(callback: BaseDataSource.LoadSourceCallback<Void>) {}

    override fun saveData(dataModel: Void) {}

    override fun refreshData() {}

    override fun offLine() {}

    companion object {
        val TYPE_REGISTER = 1 // 发送注册验证码
        val TYPE_LOGIN = 2 // 发送登录验证码
        val TYPE_MODIFY = 3 // 发送修改密码验证码
        val TYPE_FORGET = 4 // 发送找回密码验证码
        val TYPE_MODIFY_PHONE = 5 // 发送修改手机号验证码

        var INSTANCE: RegisterRepository? = null

        fun getInstance(): RegisterRepository {
            if (INSTANCE == null)
                INSTANCE = RegisterRepository()
            return INSTANCE!!
        }
    }
}